#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <SDL/SDL.h>
#include <chunks/GuacVideo_window_setup.h>
#include "shared_setup.h"

// defined in GuacInput
extern int quitRequested;

int window_get_quitrequested(lua_State *L)
{
    lua_pushboolean(L, quitRequested);
    return 1;
}

int window_get_title(lua_State *L)
{
    char* title;
    char* icon;
    SDL_WM_GetCaption(&title, &icon);
    lua_pushstring(L, title);
    return 1;
}

int window_set_title(lua_State *L)
{
    SDL_WM_SetCaption(lua_tostring(L,2), lua_tostring(L,2));
    return 0;
}

int window_minimize(lua_State *L)
{
    SDL_WM_IconifyWindow();
    return 0;
}

luaL_Reg window_functions[] = {
    {"get_title",window_get_title},
    {"set_title",window_set_title},
    {"minimize",window_minimize},
    {"get_quitrequested",window_get_quitrequested},

    {"getmetatable", raw_getmetatable},
    {"setmetatable", raw_setmetatable},
    {"getfenv", raw_getfenv},
    {"setfenv", raw_setfenv},

    {NULL,NULL}
};

int luaopen_window(lua_State* L)
{
    lua_newtable(L);
    luaL_register(L, NULL, window_functions);

    luaL_loadbuffer(L, chunk_GuacVideo_window_setup, sizeof(chunk_GuacVideo_window_setup), NULL);
    lua_insert(L,-2);
    lua_call(L,1,1);

    lua_pushvalue(L,-1);
    lua_setglobal(L,"window");

    return 1;
}
